<?php
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
// TODO: includes -nm

/**
 * Provides functionality when dealing with a SQL database.
 *
 * @author nicholas malacarne <nicholas.malacarne@gmail.com>, kayvan sarikhani <ksarikhani@gmail.com>
 */
class DatabaseManager {
    
    // database connection -nm
    private $connection;
    
    // constructor -nm
    public function __construct( $host, $user, $pw, $db ){
        
        // create new connection -nm
        $this->connection = new mysqli( $host, $user, $pw, $db );
        
        $this->checkErrors();
        
    } // end constructor -nm
    
    // destructor -nm
    public function __destruct(){
        
        $this->connection->close();
        
    } // end destructor -nm

    // insert data into specified table -ks
    public function insertValue( $table, $col, $value ) {

        $count = sizeof( $col );
        
        $tableCol = "";
        
        $tableValue = "";
        
        for ($i = 0; $i <= ($count - 1); $i++) {
            $tableCol .= "$col[$i]";
            if ($i != ($count - 1)) {
                $tableCol .= ",";
            }
        }
        
        for ($k = 0; $k <= ($count - 1); $k++) {
            $tableValue .= "\"$value[$k]\"";
            if ($k != ($count - 1)) {
                $tableValue .= ",";
            }
        }
        
        $insertion = "INSERT INTO $table ( $tableCol ) VALUES ( $tableValue )";

        $result = $this->connection->query($insertion);

        if ( $result ) {
            return $result;
        } else {
            return null;
        }

    } // end method -ks

    // delete data from specified table -ks
    public function deleteValue( $table, $col, $value ) {

        $count = sizeof( $col );
        
        $tableCol = "";
        
        $tableValue = "";
        
        for ($i = 0; $i <= ($count - 1); $i++) {
            $tableCol .= "$col[$i]";
            if ($i != ($count - 1)) {
                $tableCol .= ",";
            }
        }
        
        for ($k = 0; $k <= ($count - 1); $k++) {
            $tableValue .= "\"$value[$k]\"";
            if ($k != ($count - 1)) {
                $tableValue .= ",";
            }
        }
        
        $deletion = "DELETE FROM $table WHERE ( $tableCol ) = ( $tableValue )";

        $result = $this->connection->query($deletion);

        if ( $result ) {
            return $result;
        } else {
            return null;
        }

    } // end method -ks

    // matches column to value and returns matched row -nm
    public function getRowByValue( $table, $col, $value ) {
        
        $value = $this->connection->escape_string( $value );
        
        $result = $this->connection->query( "SELECT * FROM $table WHERE $col = '$value'" );
        
        //print_r($result);
        
        if ( $result ){
            
            return $result->fetch_assoc();
            
        }else{
            
            return null;
        }
        
    } // end method -nm
    
    // updates a column in a row  -nm
    public function updateRowValue( $table, $target, $row, $col, $value ) {
        
        $value = $this->connection->escape_string( $value );

        $result = $this->connection->query( "UPDATE $table SET $col = '$value' WHERE $target='$row'" );
        
    } // end method -nm
    
    private function checkErrors(){
        
        if ( $this->connection->connect_errno ){
            
            throw new ErrorException( $this->connection->error );
        }
    } // end method -nm
}
